<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <div class="query-card">
        <!-- <el-row type="flex" align="middle">
        <el-col :span="12">-->
        <div class="grid-content bg-purple search">
          <el-form ref="query" :model="query" label-width="100px">
            <el-form-item style="margin-bottom:0px" label="状态">
              <el-select style="width:360px" v-model="query.type" placeholder="请选择分类">
                <el-option label="全部" value="0"></el-option>
                <el-option label="待审核" value="1"></el-option>
                <el-option label="已通过" value="2"></el-option>
                <el-option label="已拒绝" value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="margin-bottom:0px" label="关键字">
              <i slot="label" class="el-icon-search" style="font-size:20px"></i>
              <el-input style="width:360px" v-model="query.keywords" placeholder="请输入用户手机号/订单编号搜索"></el-input>
            </el-form-item>
            <el-form-item style="margin-bottom:0px" label="操作日期">
              <el-date-picker
                ref="picker"
                v-model="query.timeArr"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </div>
        <!-- </el-col> -->
        <!-- <el-col :span="12"> -->
        <div class="grid-content bg-purple-light">
          <el-button type="primary" @click="queryAsk">查询</el-button>
          <el-button @click="queryReset">重置</el-button>
        </div>
        <!-- </el-col>
        </el-row>-->
      </div>
    </el-card>
    <!-- 表格 -->
    <el-card class="box-card">
      <el-table
        :data="tableData"
        v-loading="loading"
        :header-cell-style="{background:'#eee'}"
        border
        style="width: 100%;margin-bottom: 20px;"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="nickname" label="用户昵称"></el-table-column>
        <el-table-column align="center" prop="mobile" label="手机"></el-table-column>
        <el-table-column align="center" prop="name" label="真实姓名"></el-table-column>
        <el-table-column align="center" prop="money" label="提现金额"></el-table-column>
        <el-table-column align="center" prop="sxf" label="手续费"></el-table-column>
        <el-table-column align="center" prop="s_money" label="实际打款"></el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.status==1">待审核</div>
            <div v-if="scope.row.status==2">已通过</div>
            <div v-if="scope.row.status==3">已拒绝</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作人">
          <template slot-scope="scope">
            <div v-if="scope.row.admin_id==0">-</div>
            <div v-else>{{scope.row.admin_name}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="add_time" label="申请时间"></el-table-column>
        <el-table-column align="center" prop="update_time" label="更新时间"></el-table-column>

        <el-table-column align="center" fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <div v-if="scope.row.status==1">
              <el-popconfirm
                confirmButtonText="确认"
                cancelButtonText="取消"
                icon="el-icon-info"
                iconColor="red"
                title="是否确认支付宝提现？"
                @onConfirm="zfbPay(scope.row.id)"
              >
                <el-button slot="reference" type="text" size="small">支付宝</el-button>
              </el-popconfirm>
              <el-popconfirm
                confirmButtonText="确认"
                cancelButtonText="取消"
                icon="el-icon-info"
                iconColor="red"
                title="确认是否人工处理？"
                @onConfirm="defaultCash(scope.row.id,2)"
              >
                <el-button slot="reference" style="margin-left:10px" type="text" size="small">人工处理</el-button>
              </el-popconfirm>
              <el-popconfirm
                confirmButtonText="确认"
                cancelButtonText="取消"
                icon="el-icon-info"
                iconColor="red"
                title="确认是否拒绝提现？"
                @onConfirm="defaultCash(scope.row.id,1)"
              >
                <el-button slot="reference" style="margin-left:10px" type="text" size="small">拒绝</el-button>
              </el-popconfirm>
            </div>
            <div v-else>-</div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div style="text-align:right">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="currentPage"
        :total="total"
        @current-change="currentChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { cashList, cashZFB, defaultPay } from '@/api/transition'
import { parseTime } from '@/utils/index'

export default {
  name: 'CashList',
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //query
      query: {
        keywords: '',
        type: '0',
        add_time: '',
        timeArr: []
      },
      options: '',
      tableData: [],
      loading: false,
      picktime: '1',
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', [new Date(), new Date()])
            }
          },
          {
            text: '昨天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 15)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  mounted() {
    this.query.timeArr = []
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      if (this.query.timeArr.length > 0) {
        this.query.add_time =
          parseTime(this.query.timeArr[0], '{y}-{m}-{d}') +
          ',' +
          parseTime(this.query.timeArr[1], '{y}-{m}-{d}')
      }
      const res = await cashList({
        page: this.currentPage,
        limit: this.pageSize,
        keywords: this.query.keywords,
        status: this.query.type,
        add_time: this.query.add_time
      })
      console.log(res)

      this.loading = false
      this.tableData = res.data
      this.total = res.count
      this.tableData.forEach((item) => {
        if (item.add_time == 0) {
          item.add_time = '-'
        } else {
          item.add_time = parseTime(item.add_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
        if (item.update_time == 0) {
          item.update_time = '-'
        } else {
          item.update_time = parseTime(
            item.update_time,
            '{y}-{m}-{d} {h}:{i}:{s}'
          )
        }
      })
    },
    queryAsk() {
      console.log(this.query)
      this.currentPage = 1
      this.getList()
    },
    queryReset() {
      this.resetForm('query')
      this.total = 0
      this.currentPage = 1
      this.pageSize = 10
      this.query.keywords = ''
      this.query.type = '0'
      this.query.add_time = ''
      this.query.timeArr = [new Date(), new Date()]
      this.getList()
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getList()
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 支付宝
    async zfbPay(id) {
      const res = await cashZFB({
        id
      })
      if (res.st == 1) {
        this.$message({
          message: '支付宝提现成功',
          type: 'success'
        })
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
    async defaultCash(id, type) {
      const res = await defaultPay({
        id,
        type
      })
      if (res.st == 1) {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.query-card {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input--medium .el-input__inner {
  width: 360px;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
::v-deep .el-form {
  display: flex;
}
</style>